package com.ssy.lingxi.platform.manage.controller;

import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.manage.api.model.dto.unit.UnitDTO;
import com.ssy.lingxi.platform.manage.entity.base.Unit;
import com.ssy.lingxi.platform.manage.service.IUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @menu 平台后台 - 系统管理 - 单位管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/30
 */
@RestController
@RequestMapping("/manage/unit")
public class UnitController {

    @Autowired
    private IUnitService unitService;

    /**
     * 查询单位列表
     * @param pageVO 分页实体
     * @param name 单位名称
     * @return Wrapper<PageData<Unit>>
     */
    @RequestMapping(value = "/getUnitList", method = RequestMethod.GET)
    public Wrapper<PageData<Unit>> getUnitList(PageVO pageVO, @RequestParam(value = "name", required = false) String name) {
        Page<Unit> result = unitService.getUnitList(pageVO, name);
        return Wrapper.success(new PageData<>(result.getTotalElements(),result.getContent()));
    }

    /**
     * 查询单位信息
     * @param id 单位id
     * @return Wrapper<?>
     */
    @RequestMapping(value = "/getUnit", method = RequestMethod.GET)
    public Wrapper<Unit> getUnit(@RequestParam("id") Long id) {
        return Wrapper.success(unitService.getUnit(id));
    }

    /**
     * 新增/修改单位
     * @param unitRequest 单位实体
     */
    @RequestMapping(value = "/saveOrUpdateUnit", method = RequestMethod.POST)
    public Wrapper<String> saveOrUpdateUnit(@RequestBody UnitDTO unitRequest){
        return Wrapper.success(unitService.saveOrUpdateUnit(unitRequest));
    }

    /**
     * 删除单位
     * @param id 单位id
     */
    @RequestMapping(value = "/deleteUnit", method = RequestMethod.GET)
    public Wrapper<String> deleteUnit(@RequestParam("id") Long id){
        return Wrapper.success(unitService.deleteUnit(id));
    }

    /**
     * 启用/停用单位
     * @param id 单位id
     * @param status 是否启用
     */
    @RequestMapping(value = "/updateUnitStatus", method = RequestMethod.GET)
    public Wrapper<String> updateUnitStatus(@RequestParam("id") Long id, @RequestParam("status") Boolean status) {
        return Wrapper.success(unitService.updateUnitStatus(id, status));
    }
}
